package com.songtech.server.frame.protocal.analyse;

import com.songtech.server.constant.TcpServer;
import com.songtech.server.entry.BaseProtocal;
import com.songtech.server.entry.ConfirmProtocal;
import com.songtech.server.entry.PositionProtocal;
import com.songtech.server.frame.BaseServiceRegister;
import com.songtech.server.frame.protocal.AssembleFactory;
import com.songtech.server.frame.route.Route;
import com.songtech.server.utils.FormatDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Create By YINN on 2018/4/6 16:28
 * Description : 协议分析器
 */

@Service
public class AnalyzeImpl implements Analyze {

    private final static String HEART_BEAT = "HTBT";

    //确认协议命令cmd
    private final static String CONFIRM_CMD = "V4";

    //包头起始符
    private final static String DATA_HEADER = "*";

    //一般协议params命令数组起始位置
    private final static int PARAMS_BEGINNING_INDEX = 4;

    //确认协议params命令数组起始位置
    private final static int CONFIRM_PARAMS_BEGINNING_INDEX = 6;

    //分隔符
    private final static String SEPARATOR = TcpServer.SEPARATOR;

    //日志
    private static Logger log = LoggerFactory.getLogger(AnalyzeImpl.class);


    @Autowired
    private Route route;

    @Override
    public void spiltMsg(String in) throws IllegalAccessException, InstantiationException {
        //判断是否存在开头
//        System.out.println("输入消息:"+in);
        int begin = in.indexOf(DATA_HEADER);
        if (begin > -1) {

            //获取消息体
            String msg = in.substring(begin + 1);

            String[] targets = msg.split(SEPARATOR);

            String cmd = targets[2];

            //判断命令是否为空 或者 是否为心跳包
            if (StringUtils.isBlank(cmd) || HEART_BEAT.equals(cmd)){
                return;
            }

            //普通信息
            int size = targets.length;

            if (isConfirmMsg(cmd)) {

                //命令数组长度
                String[] params = new String[size - CONFIRM_PARAMS_BEGINNING_INDEX];

                //拆解命令数组
                int num = 0;
                for (int i = CONFIRM_PARAMS_BEGINNING_INDEX; i < size; i++) {
                    params[num] = targets[i];
                    num++;
                }

                ConfirmProtocal confirmProtocal = new ConfirmProtocal(targets[0], targets[1], cmd, targets[3], FormatDateUtils.parseToLocalTime(targets[4]), FormatDateUtils.parseToLocalTime(targets[5]), params);

                route.confirm(confirmProtocal);

            } else {


                //命令数组长度
                String[] params = new String[size - PARAMS_BEGINNING_INDEX];

                //拆解命令数组
                int num = 0;
                for (int i = PARAMS_BEGINNING_INDEX; i < size; i++) {
                    params[num] = targets[i];
                    num++;
                }

                //调用装配工程，获取装配后的实例
                BaseProtocal entry = AssembleFactory.assemble(BaseServiceRegister.getBeanClass(cmd).newInstance(),
                        targets[0], targets[1], cmd, FormatDateUtils.parseToLocalTime(targets[3]), params);

                route.dispense(entry);

//                //路由分发
//                route.dispense(targets[0], targets[1], targets[2], FormatDateUtils.parseToLocalTime(targets[3]), params);
            }

        } else {

            log.error("此条数据包内不存在包头开始分隔符,内容如下:" + in);

        }

    }

    private boolean isConfirmMsg(String cmd) {

        if (CONFIRM_CMD.equals(cmd)) {
            return true;
        }

        return false;
    }

//    /**
//     * Created by YINN on 2018/4/9 16:10.
//     *
//     * @param msg
//     * @Description : 获取设备imei
//     * @Result :
//     */
//    @Override
//    public String getIMEI(String msg) {
//        return msg.split(SEPARATOR)[1];
//    }
}
